package src.main.java.com.pms.gui.dialog.user;

import src.main.java.com.pms.service.UserService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ChangePasswordDialog extends JDialog {
    private JTextField usernameField;
    private JPasswordField oldPasswordField;
    private JPasswordField newPasswordField;
    private JPasswordField confirmPasswordField;
    private UserService userService;

    public ChangePasswordDialog(JFrame parent) {
        super(parent, "修改密码", true);
        userService = new UserService();
        initComponents();
    }

    private void initComponents() {
        setSize(350, 300);
        setLocationRelativeTo(getParent());

        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);

        // 用户名
        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(new JLabel("用户名:"), gbc);

        gbc.gridx = 1;
        usernameField = new JTextField(20);
        panel.add(usernameField, gbc);

        // 原密码
        gbc.gridx = 0;
        gbc.gridy = 1;
        panel.add(new JLabel("原密码:"), gbc);

        gbc.gridx = 1;
        oldPasswordField = new JPasswordField(20);
        panel.add(oldPasswordField, gbc);

        // 新密码
        gbc.gridx = 0;
        gbc.gridy = 2;
        panel.add(new JLabel("新密码:"), gbc);

        gbc.gridx = 1;
        newPasswordField = new JPasswordField(20);
        panel.add(newPasswordField, gbc);

        // 确认新密码
        gbc.gridx = 0;
        gbc.gridy = 3;
        panel.add(new JLabel("确认新密码:"), gbc);

        gbc.gridx = 1;
        confirmPasswordField = new JPasswordField(20);
        panel.add(confirmPasswordField, gbc);

        // 按钮
        JButton changeButton = new JButton("修改");
        changeButton.addActionListener(new ChangePasswordActionListener());

        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.gridwidth = 2;
        panel.add(changeButton, gbc);

        add(panel);
        setVisible(true);
    }

    private class ChangePasswordActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String username = usernameField.getText();
            String oldPassword = new String(oldPasswordField.getPassword());
            String newPassword = new String(newPasswordField.getPassword());
            String confirmPassword = new String(confirmPasswordField.getPassword());

            if (newPassword == null || newPassword.trim().isEmpty() || confirmPassword == null || confirmPassword.trim().isEmpty()) {
                JOptionPane.showMessageDialog(ChangePasswordDialog.this, "密码不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            if (!newPassword.equals(confirmPassword)) {
                JOptionPane.showMessageDialog(ChangePasswordDialog.this, "两次输入的新密码不一致！", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            try {
                if (userService.changePassword(username, oldPassword, newPassword)) {
                    JOptionPane.showMessageDialog(ChangePasswordDialog.this, "密码修改成功！");
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(ChangePasswordDialog.this, "密码修改失败，原密码可能错误！", "错误", JOptionPane.ERROR_MESSAGE);
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(ChangePasswordDialog.this, "密码修改失败：" + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}
